package com.pai4j.pgc.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.pai4j.common.enums.RedisKeyEnum;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.common.util.RedisUtil;
import com.pai4j.domain.vo.response.UserBaseResponseInfoVO;
import com.pai4j.domain.vo.response.subjectaccount.ArticleBaseResponseVO;
import com.pai4j.domain.vo.response.subjectaccount.SubjectBaseResponseVO;
import com.pai4j.pgc.dao.ISubjectDAO;
import com.pai4j.pgc.entity.ArticleEntity;
import com.pai4j.pgc.entity.SubjectEntity;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * *
 *
 * @BelongsProject: pai4j
 * @BelongsPackage: com.pai4j.pgc.service
 * @Author: CYM-pai
 * @CreateTime: 2025-05-16  18:01
 * @Description: TODO
 * @Version: 1.0
 */

@Service
public class SubjectAccountService {
    
    
    @Resource
    private RedisUtil redisUtil;
    
    @Resource
    private ISubjectDAO subjectDAO;
    
    @Resource
    private AccountService accountService;
    
    /**
     *  获取当前用户（作者）的专题列表
     *
     * @author: CYM-pai
     * @date: 2025/05/10 20:28
     **/
    
    public List<SubjectBaseResponseVO> getSubjectListFromCache(String author) {
        String key = RedisKeyEnum.SUBJECT_AUTHOR_LIST_CACHE.getKey(author);
        String cache = redisUtil.get(key);
        if (StringUtils.isNotBlank(cache)) {
            return JsonUtil.fromJson(cache, new TypeReference<List<SubjectBaseResponseVO>>() { });
        }
        return this.refreshSubjectListCache(author);
    }
    
    
    /**
     *  刷新该用户下专题列表缓存（只查询未删除的专题）
     *
     * @author: CYM-pai
     * @date: 2025/05/10 20:16
     **/
    public List<SubjectBaseResponseVO> refreshSubjectListCache(String author){
        String keyAuthor = RedisKeyEnum.SUBJECT_AUTHOR_LIST_CACHE.getKey(author);
        // 只查询未删除的专题
        List<SubjectEntity> list = subjectDAO.findByAuthorAndDeleteFlag(
                author, 
                com.pai4j.common.enums.DeleteFlagCodeEnum.UN_DELETE.getCode());
        UserBaseResponseInfoVO userBaseResponseInfoVO = accountService.accountInfo(author);
        List<SubjectBaseResponseVO> subjectBaseResponseVOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            subjectBaseResponseVOList = list.stream().map(e -> {
                SubjectBaseResponseVO subjectBaseResponseVO;
                subjectBaseResponseVO = convertBaseVo(e);
                subjectBaseResponseVO.setUserInfo(userBaseResponseInfoVO);
                return subjectBaseResponseVO;
            }).collect(Collectors.toList());
        }
        redisUtil.set(keyAuthor, JsonUtil.toJsonString(subjectBaseResponseVOList), RedisKeyEnum.SUBJECT_AUTHOR_LIST_CACHE.getExpireTime());
        return subjectBaseResponseVOList;
    }
    
    
    /**
     * 将SubjectEntity转换为SubjectBaseResponseVO
     *
     * @author: CYM-pai
     * @date: 2025/05/16 18:10
     **/
     public SubjectBaseResponseVO convertBaseVo(SubjectEntity e) {
        
        // 创建专题响应VO对象实例
        SubjectBaseResponseVO subjectBase = new SubjectBaseResponseVO();
        
        // 使用Spring的Bean工具类进行属性复制，减少手动赋值的繁琐
        BeanUtils.copyProperties(e, subjectBase);
        
        return subjectBase;
    }
    
    public ArticleBaseResponseVO convertBaseVo(ArticleEntity e) {
        
        // 创建专题响应VO对象实例
        ArticleBaseResponseVO articleBase = new ArticleBaseResponseVO();
        
        // 使用Spring的Bean工具类进行属性复制，减少手动赋值的繁琐
        BeanUtils.copyProperties(e, articleBase);
        
        // 确保 cover 不为 null
        if (articleBase.getCover() == null) {
            articleBase.setCover(new ArrayList<>());
        }
        
        articleBase.setPlanPushTime(e.getPlanPushDate().getTime());
        articleBase.setPublishTime(e.getPlanPushDate().getTime());
        return articleBase;
    }
    
}
